package com.project.communitymanagement.controller;

import com.project.communitymanagement.common.BaseController;
import com.project.communitymanagement.common.Page;
import com.project.communitymanagement.common.ResponseEntity;
import com.project.communitymanagement.entity.TbOwner;
import com.project.communitymanagement.service.TbOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ProjectName: community-management
 * @Package: com.project.communitymanagement.controller
 * @ClassName: TbOwnerController
 * @Author: "XGP"
 * @Description:
 * @Date: 2022/3/13 12:32
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("tbowner")
public class TbOwnerController extends BaseController<TbOwnerService, TbOwner> {

    @Autowired
    private TbOwnerService tbownerService;

    @Override
    protected TbOwnerService service() {
        return tbownerService;
    }

    @PostMapping("reportList")
    public ResponseEntity<Page> reportList(@RequestBody Page<TbOwner> data) {
        return ResponseEntity.success(tbownerService.reportList(data));
    }


    @PostMapping("reportListDown")
    public ResponseEntity<String> reportListDown(@RequestBody Page<TbOwner> data) {
        return ResponseEntity.success(tbownerService.reportListDown(data));
    }

    /**
     * @param fileName     想要下载的文件的路径
     * @param response
     * @功能描述 下载文件:
     */
    @RequestMapping("/download")
    public void download(String fileName, HttpServletResponse response,String name) {
        try {
            // path是指想要下载的文件的路径
            File file = new File("C:\\images\\" + fileName);
            // 获取文件名
            String filename = file.getName();
            // 获取文件后缀名
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            // 将文件写入输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

// 清空response
            response.reset();
// 设置response的Header
            response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }



    @GetMapping("getAll")
    public ResponseEntity<Page> getAll(){
        return ResponseEntity.success(tbownerService.getAll());
    }

    @PostMapping(value = "/fileUpload")
    public Map fileUpload(@RequestParam(value = "file") MultipartFile file) {
        Map map = new HashMap();
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "C:\\images\\";
//        String filePath = "/usr/local/tomcat/jar/images/";
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("filename", fileName);
        return map;
    }


    @GetMapping(value = "/images",produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[]  images(@RequestParam("fileName") String fileName) throws IOException {
//        File file = new File("/usr/local/tomcat/jar/images/" + fileName);
        File file = new File("C:\\images\\" + fileName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }


    @GetMapping(value = "/getDataByHome")
    public ResponseEntity getDataByHome() {
        return  ResponseEntity.success(service().getDataByHome());
    }

}

